package niconama04.server;

import java.util.List;

import niconama04.client.RegistSentenceService;
import niconama04.model.impl.TransacterFactory;
import niconama04.parser.ParseResult;
import niconama04.parser.impl.ParserFactory;
import niconama04.shared.data.Constants;
import niconama04.shared.data.Grammer;
import niconama04.shared.data.Sentence;
import niconama04.shared.data.Word;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;


public class RegistSentenceServiceImpl extends RemoteServiceServlet implements RegistSentenceService {

	/** Serial Version */
	private static final long serialVersionUID = -3958916532732607224L;

	@Override
	public Sentence registServer(String x_value) throws Exception {
		Sentence sentence = null;
		try {
			ParseResult parserResult = ParserFactory.getInstance().getParser().parse(x_value);
			sentence = new Sentence();
			List<Word> words = parserResult.getWords();
			Grammer grammer = new Grammer();
			grammer.setConfiguration(Constants.getConfiguration(words));
			sentence.setSentence(x_value);
			sentence.setGrammer(grammer);
			sentence.setWords(words);
			if (!words.isEmpty()) {
				TransacterFactory.getInstance().getTransacter().registSentence(sentence);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return sentence;
	}
}
